<template>
  <div>
    <work-template @submitOn="submitOn" :flowId="flowId"
                   @closeSelectedTag="closeSelectedTag" :butShow="bool">
      <div slot="form" class="container-div">
        <decoration-basic-info v-model="detailForm" :state="isEdit"/>
        <decoration-message v-model="detailForm" ref="refDetailForm" :state="isEdit"/>
        <!--        <Information-entry v-model="detailForm" />-->
      </div>
    </work-template>

  </div>
</template>

<script>
import {
  getToken
} from "@/utils/auth";
import {
  getByCarId,
  getDecorateInfoByWorkId,
  addDecorate,
  updateDecorate,

} from "@/api/cwgd/decorationApi.js";
import {
  getWorkOrder
} from "@/api/cwgd/cwgdApi.js";


import LinkageSelect from "../../components/linkageSelect";
// import updataFile from "./updataFile.vue";
import decorationBasicInfo from "../components/decoration/decorationBasicInfo.vue"
import decorationMessage from "../components/decoration/decorationMessage.vue"
import InformationEntry from "../components/decoration/InformationEntry.vue"

export default {
  dicts: ['work_order_type', 'online_offline'],
  name: "DecorationDetails",
  components: {
    decorationBasicInfo,
    decorationMessage,
    InformationEntry
  },

  data() {
    return {

      isForm: true,
      method: {},
      inspectDetail: {},
      // 表单参数
      detailForm: {
        workId: '',
        businessDataId: "",
        expectEndTime: "",
        expectStartTime: "",
        price: "",
        supplierId:"",
        supplierName: "",
        proList: [],

        workOrderStatus: "",
        remainingTotalBudget: "",
        totalBudget: "",
        actualStartTime: "",
        actualEndTime: "",
        payType: "",
        feedbackAdvancePayment: "",
        photoUrl: "",//装饰完成照片
        voucherUrl: "",//结算单上传
        view: "",//装饰意见反馈
      },

    };
  },
  watch: {},
  computed: {
    isShow() {

      return true

    },
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      if (this.$route.query.flowId) {
        return this.$route.query.flowId
      }
    }

  },
  created() {
    this.method = this.$route.query
    if (this.$route.query.id) {
      this.getByCarId()
    }

    let workOrderId = this.method.workOrderId || ''
    if (workOrderId) {
      this.getDecorate();
    }
  },
  mounted() {

  },
  methods: {
    getByCarId() {
      getByCarId(this.method.id).then((res) => {
        for (const key in res.data) {
          this.$set(this.detailForm, key, res.data[key])
        }
        this.detailForm.price = 0
        let arr = _.cloneDeep(res.data.decorateProList)
        this.$set(this.detailForm, 'proList', arr)
      })
    },


    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {

        this.$router.push({
          path: "/cwgd/decorationList",
        })
      })
    },

    // 提交操作
    submitOn() {
      this.$refs.refDetailForm.handleEffect().then(() => {
        const loading = this.$loading({
          lock: true,
          text: '保存中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        this.detailForm.workId = this.method.id
        this.detailForm.carId = this.method.carId
        this.detailForm.workOrderStatus = "2"
        addDecorate(this.detailForm).then((res) => {
          if (res.code === 200) {
            loading.close();
            this.closeSelectedTag(this.$route)
          }
        }).catch(() => {
          loading.close();
        })

      }).catch(() => {
      })

    },

    // 详情回显
    getDecorate() {
      let id = this.method.workOrderId
      getDecorateInfoByWorkId(id).then(res => {
        if (res.code === 200) {
          this.isForm = true
          this.detailForm = res.data
          this.$set(this.detailForm,'decorateProList',res.data.proList)
          // if (this.detailForm.voList && this.detailForm.voList.length) {
          //   this.detailForm.voList.forEach((v) => {
          //     v.disabled = false
          //   })
          // }

        }
      })
    }
  }
};
</script>


<style lang="scss" scoped>
@import "@/styles/cwgd/container.scss";
</style>
